<template>
  <div style="padding: 10px">
    <!--   功能区域-->
    <div style="margin: 10px">
      <el-button type="primary" @click="add">新增</el-button>
    </div>

    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入书籍名称" style="width:20%" clearable />
      <el-button type="primary" style="margin-left:5px" @click="load"><el-icon><Search /></el-icon><span>查询</span></el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="序号" sortable width="180" />
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column prop="author" label="作者" width="180" />
      <el-table-column prop="publish" label="出版社" width="200" />
      <el-table-column prop="price" label="价格" width="180" />
      <el-table-column prop="createtime" label="出版日期" width="180" />
      <el-table-column prop="type" label="类型" width="180" />
      <el-table-column label="封面" >
        <template #default="scope">
          <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.cover"
              :preview-src-list="[scope.row.cover]"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button  size="small" type="info" @click="details(scope.row)">简介</el-button>
          <el-button  size="small" type="success" @click="handleEdit(scope.row)">编辑</el-button>
          <!--弹出对话框-->
          <el-popconfirm title="你确定要删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small" type="danger" >删除</el-button>
            </template>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />

      <!--弹出对话框-->
      <el-dialog v-model="dialogVisible" title="提示" width="30%">
        <el-from  :model="form" label-width="120px">
          <el-form-item label="书籍名称">
            <el-input v-model="form.name" style="width:80%" />
          </el-form-item>
          <el-form-item label="书籍作者">
            <el-input v-model="form.author" style="width:80%" />
          </el-form-item>
          <el-form-item label="书籍价格">
            <el-input v-model="form.price" style="width:80%" />
          </el-form-item>
          <el-form-item label="出版时间">
            <el-date-picker v-model="form.createtime" value-format="YYYY-MM-DD" type="date" style="width: 80%" clearable></el-date-picker>
          </el-form-item>
          <el-form-item label="出&nbsp;版&nbsp;社&nbsp;" >
            <el-input v-model="form.publish" style="width:80%" />
          </el-form-item>
          <el-form-item label="书籍类型" >
            <el-input v-model="form.type" style="width:80%" />
          </el-form-item>
          <el-form-item label="封面" >
            <el-upload ref="upload" action="http://localhost:9090/files/upload" :on-success="filesUploadSuccess">
              <el-button type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>

          <div id="div1"></div>

        </el-from>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">
              确 定
            </el-button>
          </span>
        </template>
      </el-dialog>


    </div>
    <el-dialog v-model="vis" title="详情" width="50%">
      <el-card>
        <div v-html="detail.synopsis" style="min-height: 100px"></div>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import E from 'wangeditor';
import request from "../../utils/request";
import {Search} from '@element-plus/icons';

let editor;

export default {
  name: 'Book',
  components: {
    Search
  },
  data(){
    return{
      form: {},
      dialogVisible: false,
      search:'',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData:[],
      vis: false,
      detail: {},
    }
  },
  created(){
    this.load()
  },
  methods:{
    details(row) {
      this.detail = row
      this.vis = true
    },
    filesUploadSuccess(res) {
      console.log(res)
      this.form.cover = res.data
    },
    load(){
      request.get("/api/book", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    add(){
      this.dialogVisible = true
      this.form = {}
      this.$refs['upload'].clearFiles()   //清除历史文件

      this.$nextTick(() => {
        // 关联弹窗里面的div，new一个 editor对象
        if (!editor) {
          editor = new E('#div1')

          // 配置 server 接口地址
          editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
          editor.config.uploadFileName = "file"  // 设置上传参数名称
          editor.create()
        }
        editor.txt.html("")
      })
    },
    save(){
      this.form.synopsis = editor.txt.html()   //获取编辑器里面的值，然后放到实体中

      if(this.form.id){ //更新
        request.put ("/api/book",this.form).then(res => {
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type:"success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()   //刷新表格数据
          this.dialogVisible = false  //关闭弹窗
        })
      }
      else{   //新增
        request.post("/api/book",this.form).then(res => {
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()   //刷新表格数据
          this.dialogVisible = false  //关闭弹窗
        })
      }
    },
    handleEdit(row){    //编辑
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick( () => {
      //关联div里面的div.new的一个editor对象
        if(!editor){
          editor = new E('#div1')

          // 配置 server 接口地址
          editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
          editor.config.uploadFileName = "file"  // 设置上传参数名称
          editor.create()
        }
        editor.txt.html(row.synopsis)
      })
    },
    handleDelete(id){   //删除
      console.log(id)
      request.delete("/api/book/" + id).then(res => {
        if(res.code === '0'){
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()   //删除之后重新加载表格
      })
    },
    handleSizeChange(){   //改变当前每页的个数触发
      this.load()
    },
    handleCurrentChange(){    //改变当前页码触发
      this.load()
    }
  }
}
</script>
